package sanrenxing.dayEnd.backRepertoryTotal.dao;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import sanrenxing.dayEnd.backRepertoryTotal.domain.CashAccount;
import sanrenxing.dayEnd.backRepertoryTotal.domain.TransferInstruction;

import java.util.List;

/**
 * 划款指令数据层接口
 */
@Repository
public interface TransferInstructionDao {
    /**
     * 增加一行数据到数据库中
     * @param transferInstruction 划款对象
     * @return 0：添加成功，1：添加失败
     */
    @Insert("insert into transferInstruction values(seqTransferInstruction.nextval,#{caIds},#{money},to_date('${tranDate}','yyyy-MM-dd'),to_date('${arrival}','yyyy-MM-dd')+1,#{purpose},#{caId})")
    public int add(TransferInstruction transferInstruction);

    /**
     * 从数据库中删除一行数据
     * @param tranId 划款编号
     * @return 0：删除成功，1：删除失败
     */
    @Delete("delete from transferInstruction where tranId=#{tranId}")
    public int delete(int tranId);

    /**
     * 修改一行数据到数据库
     * @param transferInstruction 划款对象
     * @return 0：修改成功，1：修改失败
     */
    @Update("update transferInstruction set caIds=#{caIds},money=#{money},tranDate=#{tranDate},arrival=#{arrival},purpose=#{purpose},caId=#{caId} where tranId=#{tranId}")
    public int update(TransferInstruction transferInstruction);

    /**
     * 根据条件分页查询
     * @param start  页数
     * @param rows  行数
     * @param condition 条件
     * @return
     */
    @Select("select * from(select a.*,rownum rn from (select * from transferInstruction) a where rownum < #{rows}) where rn>#{start} and 1=1 ${condition}")
    public List<TransferInstruction> findAll(@Param("start") int start, @Param("rows") int rows, @Param("condition") String condition);

    /**
     * 根据条件查询记录统计数
     * @param condition 条件
     * @return
     */
    @Select("select count(*) from transferInstruction where 1=1 ${condition}")
    public int count(@Param("condition") String condition);

    /**
     * 通过id查询单行
     * @param tranId
     * @return
     */
    @Select("select * from transferInstruction where tranId=#{tranId}")
    public TransferInstruction selectById(@Param("tranId") String tranId);

    /**
     * 通过id查询现金账户信息
     * @param caId
     * @return
     */
    @Select("select * from cashAccount where caId=#{caId}")
    public CashAccount selectCashById(String caId);
}
